package st.learnopengl.apps.assets

import gln.identifiers.GlProgram
import org.lwjgl.assimp.*

class Material(private val material: AIMaterial) {

    val ambientColor: AIColor4D = AIColor4D.create()
    val diffuseColor: AIColor4D = AIColor4D.create()
    val specularColor: AIColor4D = AIColor4D.create()

    val textures: ArrayList<Texture> = ArrayList()

    init {
        val textureCount = Assimp.aiGetMaterialTextureCount(material, Assimp.aiTextureType_DIFFUSE)

        for (i in 0 until textureCount) {
            val aiPath = AIString.calloc()
            Assimp.aiGetMaterialTexture(material, Assimp.aiTextureType_DIFFUSE, i, aiPath, null as IntArray?, null, null, null, null, null)

            textures.add(Texture(aiPath.dataString(), i))
        }

        Assimp.aiGetMaterialColor(material, Assimp.AI_MATKEY_COLOR_AMBIENT, Assimp.aiTextureType_NONE, 0, ambientColor)
        Assimp.aiGetMaterialColor(material, Assimp.AI_MATKEY_COLOR_DIFFUSE, Assimp.aiTextureType_NONE, 0, diffuseColor)
        Assimp.aiGetMaterialColor(material, Assimp.AI_MATKEY_COLOR_SPECULAR, Assimp.aiTextureType_NONE, 0, specularColor)
    }

    fun bind(program: GlProgram) {
        textures.forEach {
            it.bind(program)
        }
    }

    fun unbind() {
        textures.forEach {
            it.unbind()
        }
    }

    fun build() {
        textures.forEach {
            it.build()
        }
    }
}